<template>
  <div class="product-con2">
      <el-card class="box-card">
        <div class="clearfix">
            <span> 商品分类列表</span>
            <el-button style="float: right" type="primary" size="mini" @click="goadd"  >添加</el-button >
            <el-button style="float: right" type="primary" size="mini" v-if="parent_id != 0" @click="goParentLevel">返回上一级</el-button>
        </div>
      </el-card>

    
    <el-table :data="cateList" border style="width: 100%" class="listcc" :cell-style="{textAlign:'center'}" :header-cell-style="{'text-align':'center'}">
      <el-table-column prop="id" label="编号" width="80"></el-table-column>
      <el-table-column prop='name' label="分类名称" width="180"></el-table-column>
      <el-table-column label="级别" width="180">
        <template slot-scope='scope'>
          {{scope.row.level | levelF}}
        </template>
      </el-table-column>
      <el-table-column prop='product_count' label="商品数量" width="80"></el-table-column>
      <el-table-column prop='product_unit' label="数量单位" width="80"></el-table-column>
      <el-table-column label="是否显示" width="80" align="center">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.show_type" :active-value="1" :inactive-value="0"></el-switch>
        </template>
      </el-table-column>
      <el-table-column prop="nav_status" label="排序" width="80"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="primary"  @click="showNextLevel(scope.row.id)" size="mini" :disabled="scope.row.level != 0">查看下级</el-button>
        </template>
      </el-table-column>
     <el-table-column label="操作" width="180">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="updateProduct(scope.row.id)">编辑</el-button >
          <el-button type="danger" size="mini" @click="delProduct(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    
    </el-table> 

    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="listQuery.pageNum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="listQuery.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
    </el-pagination>
  </div>
</template>

<script>
import {fetchProductcateList} from '@/api/product.js'
// import axios from 'axios'
export default {
  name:'productcate',
  data:function(){
    return{
      listQuery:{
        pageSize: 5, //默认每页显示10条
        pageNum: 1, //默认当前页是第一页
      },
      cateList:[],
      total:0,
      parent_id:0//默认顶级分类
    }
    
  },

  // async created() {
  //   var res = await fetchProductcateList();
  //   this.cateList = res.data;
  //   this.getProductList()
  // },
  created() {
    this.getProductCateList();
  },
  filters:{
    levelF:function(value){
      return value ? '二级' :"一级";
    }
  },

  methods:{
    getProductCateList:async function (){
      var res = await fetchProductcateList(this.listQuery,this.parent_id)
      this.cateList= res.data
      this.total= res.total
    },
    goadd:function(){
        this.$router.push('/pms/productCateAdd')
    },

    handleClick: function (row) {
      console.log(row);
    },
    handleSizeChange(val) {
    console.log("修改页面条数的函数运行了", val);
    this.listQuery.pageSize = val;
    
    this.listQuery.pageNum = 1;
    //获取数据
    this.getProductCateList();
    },
    handleCurrentChange(val) {
      console.log("修改当前页的页码的函数运行了", val);
      //将页码信息 保存到  listQuery
      this.listQuery.pageNum = val;
      //再次获取数据
      this.getProductCateList();
    },
    showNextLevel: function (id) {
      this.parent_id = id;
      //重新获取数据
      this.getProductCateList();
    },
    //返回上一级
    goParentLevel: function () {
      this.parent_id = 0;
      //重新获取数据
      this.getProductCateList();
    },
    //跳转到 修改页面
    updateProduct: function (id) {
      console.log(id);
      this.$router.push("/pms/productCateUpdate/"+id);
    },
    //删除的函数
      delProduct: function (id) {
      console.log(id);
      //1.删除前需要提示
      this.$confirm("此操作将永久删除该条商品数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delProduct({ id: id }).then((res) => {
            console.log(res);
            if (res.type == "success") {
              this.$message({
                type: "success",
                message: "删除成功!",
                duration: 2000,
              });
              //2.删除后需要刷新数据
              //重新获取数据
              this.getProductList();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
            duration: 2000,
          });
        });
    },
  },

}
</script>

<style  lang='scss'>
*{
  padding: 0;
  margin: 0;
}
.product-con2{
  padding: 10px;
  .box-card{
    margin-bottom: 10px;
  }
}

</style>